package com.ywm.eduService.service.impl;


import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.databind.util.BeanUtil;
import com.ywm.eduService.entity.EduCourse;
import com.ywm.eduService.entity.EduCourseDescription;
import com.ywm.eduService.entity.frontVo.CourseFrontVo;
import com.ywm.eduService.entity.frontVo.CourseWebVo;
import com.ywm.eduService.entity.vo.CourseInfoVo;
import com.ywm.eduService.entity.vo.CoursePublishVo;
import com.ywm.eduService.mapper.EduCourseMapper;
import com.ywm.eduService.service.EduChapterService;
import com.ywm.eduService.service.EduCourseDescriptionService;
import com.ywm.eduService.service.EduCourseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ywm.eduService.service.EduVideoService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2020-05-18
 */
@Service
public class EduCourseServiceImpl extends ServiceImpl<EduCourseMapper, EduCourse> implements EduCourseService {
    @Autowired
    private EduCourseDescriptionService courseDescriptionService;

    //注入小节
    @Autowired
    private EduVideoService videoService;

    //注入章节
    @Autowired
    private EduChapterService chapterService;

    //注入描述
    @Autowired
    private EduCourseDescriptionService descriptionService;
    /**
     * 添加课程基本信息
     *
     * @param courseInfoVo
     */
    @Override
    public String saveCourseInfo(CourseInfoVo courseInfoVo) throws Exception {
        //1.向课程表添加基本信息。
        EduCourse eduCourse = new EduCourse();
        //将一个对象的值复制到另一个对象(CourseInfoVo -> EduCourse)
        BeanUtils.copyProperties(courseInfoVo, eduCourse);
        //返回影响行数
        int insert = baseMapper.insert(eduCourse);
        if (insert == 0) {
            throw new Exception("添加课程信息失败");
        }
        //2.像课程添加课程简介
        EduCourseDescription ecd = new EduCourseDescription();
        ecd.setId(eduCourse.getId());
        ecd.setDescription(courseInfoVo.getDescription());
        boolean save = courseDescriptionService.save(ecd);
        if (!save) {
            throw new Exception("添加课程简介失败");
        }
        return eduCourse.getId();
    }

    //根据课程id查询课程基本信息
    @Override
    public CourseInfoVo getCourseInfoById(String courseId) {
        //1.查询课程表
        EduCourse eduCourse = baseMapper.selectById(courseId);
        CourseInfoVo courseInfoVo = new CourseInfoVo();
        //将eduCourse的值赋值给courseInfoVo
        BeanUtils.copyProperties(eduCourse,courseInfoVo);
        //2.查询描述表
        EduCourseDescription cd = courseDescriptionService.getById(courseId);
        courseInfoVo.setDescription(cd.getDescription());
        return courseInfoVo;
    }
    //修改课程信息
    @Override
    public void updateCourseInfo(CourseInfoVo courseInfoVo) throws Exception {
        EduCourse eduCourse = new EduCourse();
        BeanUtils.copyProperties(courseInfoVo,eduCourse);
        //修改课程信息
        int i = baseMapper.updateById(eduCourse);
        if(i==0){
            throw new Exception();
        }
        //修改描述表
        EduCourseDescription description = new EduCourseDescription();
        description.setId(courseInfoVo.getId());
        description.setDescription(courseInfoVo.getDescription());
        courseDescriptionService.updateById(description);
    }

    @Override
    public CoursePublishVo getPublishCourseInfo(String id) {
        return baseMapper.getPublishCourseInfo(id);
    }
    //删除课程
    @Override
    public void removeCourse(String id) throws Exception {
        //1.删除小节
        videoService.removeVideoByCourseId(id);
        //2.删除章节
        chapterService.removeChapterVyCoursrId(id);
        //3.删除描述
        courseDescriptionService.removeById(id);
        //4.删除课程
        int i = baseMapper.deleteById(id);
        if(i==0){
            throw new Exception("删除课程失败");
        }
    }
    @ApiOperation("查询前八个热门课程")
    @Override
    @Cacheable(key = "'selectCourseList'",value = "course")//开启缓存
    public List<EduCourse> getList() {
        QueryWrapper<EduCourse> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("id");
        wrapper.last("limit 8");
        return baseMapper.selectList(wrapper);
    }

    /**
     * 条件查询+分页查询（前台）
     * @param page1
     * @param courseFrontVo
     * @return
     */
    @Override
    public Map<String, Object> getCourseFrontList(Page<EduCourse> page1, CourseFrontVo courseFrontVo) {
        QueryWrapper<EduCourse> wrapper = new QueryWrapper<>();
        //判断条件是否为空
        if(!StringUtils.isEmpty(courseFrontVo.getSubjectParentId())){
            //判断一级分类是否存在
            wrapper.eq("subject_parent_id",courseFrontVo.getSubjectParentId());
        }
        if(!StringUtils.isEmpty(courseFrontVo.getSubjectId())){
            //判断二级分类是否存在
            wrapper.eq("subject_id",courseFrontVo.getSubjectId());
        }
        if(!StringUtils.isEmpty(courseFrontVo.getBuyCountSort())){
            //关注度排序
            wrapper.orderByDesc("buy_count");
        }
        if(!StringUtils.isEmpty(courseFrontVo.getPriceSort())){
            //价格排序
            wrapper.orderByDesc("price");
        }
        if(!StringUtils.isEmpty(courseFrontVo.getGmtCreateSort())){
            //时间排序
            wrapper.orderByDesc("gmt_create");
        }
        baseMapper.selectPage(page1,wrapper);
        List<EduCourse> records = page1.getRecords();
        long current = page1.getCurrent();
        long pages = page1.getPages();
        long size = page1.getSize();
        long total = page1.getTotal();
        boolean hasNext = page1.hasNext();
        boolean hasPrevious = page1.hasPrevious();

        Map<String, Object> map = new HashMap<String, Object>();
        map.put("items", records);
        map.put("current", current);
        map.put("pages", pages);
        map.put("size", size);
        map.put("total", total);
        map.put("hasNext", hasNext);
        map.put("hasPrevious", hasPrevious);
        baseMapper.selectPage(page1,wrapper);
        System.out.println(map);
        return map;
    }

    @Override
    public CourseWebVo getBaseCourseInfo(String courseId) {
        return baseMapper.getBaseCourseInfo(courseId);
    }
}
